﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DevExpress.Web.ASPxGridView;
using DAL;

public partial class AnalysisReport_SaleResult : Frame.Pages.BasePage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        gridView.Font.Size = FontUnit.Parse("10pt");
        gridView.SettingsPager.PageSize = 40;

        if (!IsPostBack && !IsCallback)
        {
            datStart.Date = DateTime.Today;
            datEnd.Date = DateTime.Today;
            Session["sqlstr"] = null;
            Session["sqlParams"] = null;
        }

        DDataBind();
    }

    DataTable table;
    public DataTable Table
    {
        get
        {
            if (Session["sqlstr"] != null && Session["sqlParams"] != null)
            {
                table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(Session["sqlstr"].ToString(), Session["sqlParams"] as SqlParameter[]);
            }
            return table;
        }
    }

    void DDataBind()
    {
        if (Session["sqlstr"] != null && Session["sqlParams"] != null)
        {
            gridView.DataSource = Table;
            gridView.DataBind();
        }
    }

    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (String.IsNullOrEmpty(txtSalesPerson.Text.Trim()))
        {
            throw new Exception("销售人员姓名不能为空！");
        }
        string Sqlstr = @"select  a.BookID,
                                  c.PeriodName,
                                  a.BookDate,
                                  a.AddTime,
                                  a.BookNo,
                                  a.[Money],
                                  a.ContractName,
                                  a.ContractPhoneNum, 
                                  d.UserName,
                                  COUNT(b.Table_ID) as TableTotal,
                                  a.PeopleNumber,
                                  e.[Money] as ConsumerAmount
                            from  BLI_Book a LEFT OUTER JOIN BLI_ConsumerHis e on a.BookNo=e.BookNo,
                                  BLI_BookDetail b ,
                                  BI_ConsumePeriod c,
                                  BI_User d
                           where  a.SalesPerson=@SalesPerson and
                                  a.AddTime >=@bookBeginDate and
                                  a.AddTime <@bookEndDate and  
                                  a.BookNo=b.BookNo and
                                  a.Perid_ID=c.PeriodID and
                                  a.Operator=d.UserID
                        group by  a.BookID,
		                          c.PeriodName,
                                  a.BookDate,
                                  a.AddTime,
                                  a.BookNo,
                                  a.[Money],
                                  a.ContractName,
                                  a.ContractPhoneNum,
                                  d.UserName,
                                  a.PeopleNumber,
                                  e.[Money]";
        List<SqlParameter> Params = new List<SqlParameter>();
        Params.Add(new SqlParameter("@SalesPerson", txtSalesPerson.Text.Trim()));
        Params.Add(new SqlParameter("@bookBeginDate", datStart.Date));
        Params.Add(new SqlParameter("@bookEndDate", datEnd.Date.AddDays(1)));
        Session["sqlstr"] = Sqlstr;
        Session["sqlParams"] = Params.ToArray();
        gridView.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(Sqlstr, Params.ToArray());
        gridView.DataBind();
    }

    protected void btnPrint_Click(object sender, EventArgs e)
    {
        string fileName = txtSalesPerson.Text.Trim() + "_" + datStart.Date.ToString("yyyyMMdd") + "—" + datEnd.Date.ToString("yyyyMMdd");
        GridViewExporter.WriteXlsToResponse(fileName, true);
    }

    protected void DgridView_BeforePerformDataSelect(object sender, EventArgs e)
    {
        Session["BookNo"] = (sender as ASPxGridView).GetMasterRowFieldValues("BookNo");
    }

    protected override string ActionID
    {
        get { return "0"; }
    }

}